package net.xiduth;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.xiduth.game.player.Player;
import net.xiduth.game.player.Skills;
import net.xiduth.utils.Logger;
import net.xiduth.utils.SerializableFilesManager;

/**
 * 
 * Converted to Matrix by Jordan & Jae.
 * 
 */

public class DonationManager {

	public static Connection con = null;
	public static Statement stmt;
	public static boolean connectionMade;

	
	public static void XiduthDDBConnect() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		String IP="localhost"; 
        String DB="donations";
        String User="root";
        String Pass="jkayeljeoong";
			con = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);
			stmt = con.createStatement();
		} catch (Exception e) {
		Logger.log("XDDB", "Connection to database failed.");
        e.printStackTrace();
		}
	}
	
	public static void startProcess(final Player player) {
		XiduthDDBConnect();
		if (checkDonation(player.getUsername())) {
			if(player.getInventory().getFreeSlots() < 5) {
				player.getPackets().sendGameMessage("<col=00FFCC>Make sure you have at least 5 inventory slots open before doing this!");			
				return;
			}
			
			
			/**
			 * Basic Multi	
			 */
			if (checkDonationItem(player.getUsername()) == 1) {
				player.getBank().addItem(11967, 1, true);
				player.getBank().addItem(2, 500, true);
				donationGiven(player.getUsername());
				player.getPackets().sendGameMessage("You have recieved your items, they have been added to your bank!");
				
				
			/**
			 * Regular Donator Rank
			 */
			} else if (checkDonationItem(player.getUsername()) == 2) {
				player.setDonator(true);
				player.getInventory().addItem(773, 1);
				player.getInventory().addItem(774, 1);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());
				player.getPackets().sendGameMessage("You have recieved your Regular Donator Rank!");
				
				
			/**
			 * Super Donator Rank
			 */
			} else if (checkDonationItem(player.getUsername()) == 3) {
				player.setSuperDonator(true);
				player.getInventory().addItem(15078, 1);
				player.getInventory().addItem(15484, 1);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Super Donator Rank, speak with Jae for a cannon set!");
				
				
			/**
			 * Extreme Donator Rank
			 */
			} else if (checkDonationItem(player.getUsername()) == 4) {
				player.setExtremeDonator(true);
				player.getInventory().addItem(15080, 1);
				player.getInventory().addItem(15484, 1);
				player.getInventory().addItem(23030, 1);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());
				player.getPackets().sendGameMessage("You have recieved your Extreme Donator Rank, speak with Jae for a cannon set!");
				
				
			/**
			 * VIP Donator Rank
			 */
			} else if (checkDonationItem(player.getUsername()) == 5) {
				player.setVIPDonator(true);
				player.getInventory().addItem(15082, 1);
				player.getInventory().addItem(15484, 1);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your VIP Donator Rank, speak with Jae for a cannon set!");
				
			
			/**
			 * Option 6. No item.
			 */
			} else if (checkDonationItem(player.getUsername()) == 6) {
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());
				
				
			/**
			 * Dragon Claws	
			 */
			} else if (checkDonationItem(player.getUsername()) == 7) {
				player.getBank().addItem(14484, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Dragon Claws. They have been added to your bank!");
				
				
			/**
			 * Divine Spirit Shield	
			 */
			} else if (checkDonationItem(player.getUsername()) == 8) {
				player.getBank().addItem(13740, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Divine Spirit Shield. It has been added to your bank!");
				
				
			/**
			 * Elysian Spirit Shield
			 */
			} else if (checkDonationItem(player.getUsername()) == 9) {
				player.getBank().addItem(13742, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Elysian Spirit Shield. It has been added to your bank!");
				
				
			/**
			 * Arcane Spirit Shield	
			 */
			} else if (checkDonationItem(player.getUsername()) == 10) {
				player.getBank().addItem(13738, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Arcane Spirit Shield. It has been added to your bank!");
				
				
			/**
			 * Spectral Spirit Shield
			 */
			} else if (checkDonationItem(player.getUsername()) == 11  ) { // == 5) {
				player.getBank().addItem(13744, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Spectral Spirit Shield. It has been added to your bank!");
				
				
			/**
			 * Goliath Gloves
			 */
			} else if (checkDonationItem(player.getUsername()) == 12  ) { // == 15) {
				player.getBank().addItem(22358, 1, true);
				player.getBank().addItem(22359, 1, true);
				player.getBank().addItem(22360, 1, true);
				player.getBank().addItem(22361, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 4 Goliath Gloves. They have been added to your bank!");
				
				
			/**
			 * Spellcaster Gloves
			 */
			} else if (checkDonationItem(player.getUsername()) == 13 ) { // == 15) {
				player.getBank().addItem(22366, 1, true);
				player.getBank().addItem(22367, 1, true);
				player.getBank().addItem(22368, 1, true);
				player.getBank().addItem(22369, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 4 Spellcaster Gloves. They have been added to your bank!");
				
				
			/**
			 * Swift Gloves	
			 */
			} else if (checkDonationItem(player.getUsername()) == 14) {
				player.getBank().addItem(22362, 1, true);
				player.getBank().addItem(22363, 1, true);
				player.getBank().addItem(22364, 1, true);
				player.getBank().addItem(22365, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 4 Swift Gloves. They have been added to your bank!");
				
				
			/**
			 * Golden Mining Suit
			 */
			} else if (checkDonationItem(player.getUsername()) == 15 ) { // == 15) {
				player.getBank().addItem(20787, 1, true);
				player.getBank().addItem(20788, 1, true);
				player.getBank().addItem(20789, 1, true);
				player.getBank().addItem(20790, 1, true);
				player.getBank().addItem(20791, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your full Golden Mining Suit. It has been added to your bank!");
				
				
			/**
			 * Christmas Sled
			 */
			} else if (checkDonationItem(player.getUsername()) == 16 ) { // == 6) {
				player.getBank().addItem(4084, 1, true);
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Christmas Sled. It has been added to your bank!");
				
				
			/**
			 * Maxed Stats
			 */
			} else if (checkDonationItem(player.getUsername()) == 17 ) { // == 75) {
				player.getSkills().set(Skills.ATTACK, 99);
				player.getSkills().setXp(Skills.ATTACK, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.STRENGTH, 99);
				player.getSkills().setXp(Skills.STRENGTH, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.DEFENCE, 99);
				player.getSkills().setXp(Skills.DEFENCE, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.RANGE, 99);
				player.getSkills().setXp(Skills.RANGE, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.PRAYER, 99);
				player.getSkills().setXp(Skills.PRAYER, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.MAGIC, 99);
				player.getSkills().setXp(Skills.MAGIC, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.RUNECRAFTING, 99);
				player.getSkills().setXp(Skills.RUNECRAFTING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.HITPOINTS, 99);
				player.getSkills().setXp(Skills.HITPOINTS, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.AGILITY, 99);
				player.getSkills().setXp(Skills.AGILITY, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.HERBLORE, 99);
				player.getSkills().setXp(Skills.HERBLORE, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.THIEVING, 99);
				player.getSkills().setXp(Skills.THIEVING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.CRAFTING, 99);
				player.getSkills().setXp(Skills.CRAFTING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.FLETCHING, 99);
				player.getSkills().setXp(Skills.FLETCHING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.SLAYER, 99);
				player.getSkills().setXp(Skills.SLAYER, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.HUNTER, 99);
				player.getSkills().setXp(Skills.HUNTER, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.MINING, 99);
				player.getSkills().setXp(Skills.MINING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.SMITHING, 99);
				player.getSkills().setXp(Skills.SMITHING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.FISHING, 99);
				player.getSkills().setXp(Skills.FISHING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.COOKING, 99);
				player.getSkills().setXp(Skills.COOKING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.FIREMAKING, 99);
				player.getSkills().setXp(Skills.FIREMAKING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.WOODCUTTING, 99);
				player.getSkills().setXp(Skills.WOODCUTTING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.FARMING, 99);
				player.getSkills().setXp(Skills.FARMING, Skills.getXPForLevel(99));
				player.getSkills().set(Skills.SUMMONING, 99);
				player.getSkills().setXp(Skills.SUMMONING, Skills.getXPForLevel(99));
				player.getAppearence().generateAppearenceData();
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your Donation of Maxing your Stats!");
				
				
			/**
			 * Option 18. Nothing.
			 */
			} else if (checkDonationItem(player.getUsername()) == 18) {
				SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());
				
				
			/**
			 * 10 Spins
			 */
			} else if (checkDonationItem(player.getUsername()) == 19 ) { // == 2) {
				player.getSquealOfFortune().setSpins(player.getSquealOfFortune().getSpins() + 10);
		        player.refreshSqueal();
		        SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 10 Squeal of Fortune Spins!");
				
				
			/**
			 * 25 Spins
			 */
			} else if (checkDonationItem(player.getUsername()) == 20 ) { // == 5) {
				player.getSquealOfFortune().setSpins(player.getSquealOfFortune().getSpins() + 25);
		        player.refreshSqueal();
		        SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 25 Squeal of Fortune Spins!");
				
				
			/**
			 * 75 Spins
			 */
			} else if (checkDonationItem(player.getUsername()) == 21 ) { // == 10) {
				player.getSquealOfFortune().setSpins(player.getSquealOfFortune().getSpins() + 75);
		        player.refreshSqueal();
		        SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 75 Squeal of Fortune Spins!");
				
				
			/**
			 * 200 Spins
			 */
			} else if (checkDonationItem(player.getUsername()) == 22 ) { // == 25) {
				player.getSquealOfFortune().setSpins(player.getSquealOfFortune().getSpins() + 200);
		        player.refreshSqueal();
		        SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 200 Squeal of Fortune Spins!");
				
			/**
			 * 450 Spins
			 */
			} else if (checkDonationItem(player.getUsername()) == 23 ) { // == 55) {
				player.getSquealOfFortune().setSpins(player.getSquealOfFortune().getSpins() + 450);
		        player.refreshSqueal();
		        SerializableFilesManager.savePlayer(player);
				donationGiven(player.getUsername());	
				player.getPackets().sendGameMessage("You have recieved your 450 Squeal of Fortune Spins!");
			}
		} else {
			player.getPackets().sendGameMessage("<col=00FFCC>A donation under the name, " + player.getDisplayName() + ", could not be found in the database.");	
		}
	} 	
	
	/**
	 * 
	 * MySQL stuff.
	 * 
	 * @param s
	 * @return
	 * @throws SQLException
	 * 
	 */
	public static ResultSet query(String s) throws SQLException {
		try {
			if (s.toLowerCase().startsWith("select")) {
				ResultSet rs = stmt.executeQuery(s);
				return rs;
			} else {
				stmt.executeUpdate(s);
			}
			return null;
		} catch (Exception e) {
			killXDDBConnection();
		}
		return null;
	}

	public static void killXDDBConnection() {
		try {
			stmt.close();
			con.close();
		} catch (Exception e) {
			
		}
	}

	public static boolean checkDonation(String playerName) {
   
        try {
			String name2 = playerName.replaceAll("_", " ");
            Statement statement = con.createStatement();
            String query = "SELECT * FROM donation WHERE username = '" + name2 + "'";
            ResultSet results = statement.executeQuery(query);
				while(results.next()) {
                    int tickets = results.getInt("tickets"); //tickets are "Recieved" technically. 0 for claimed, 1 for not claimed.
                        if(tickets == 1) {                     
                         return true;
                         }
                                
                        }
                } catch(SQLException e) {
                        e.printStackTrace();
                }
                return false;
				
        }
	
	public static int checkDonationItem(String playerName) {
        
        try {
			String name2 = playerName.replaceAll("_", " ");
			Statement statement = con.createStatement();
            String query = "SELECT * FROM donation WHERE username = '" + name2 + "'";
            ResultSet results = statement.executeQuery(query);
                while(results.next()) {
                    int productid = results.getInt("productid");
                        if(productid >= 1) {				                          
							return productid;
                            }
								                               
                        }
                } catch(SQLException e) {
                        e.printStackTrace();
                }
				
                return 0;
				
        }		
	
	public static boolean donationGiven(String playerName) {
		try
			{
			String name2 = playerName.replaceAll("_", " "); //to fix spacing
			query("DELETE FROM `donation` WHERE username = '"+name2+"';");
            } catch (Exception e) {
            	e.printStackTrace();
					return false;
            }
                	return true;
        }
}